package com.choosefine.springboot.devops.participate.controller;


import com.choosefine.springboot.devops.activity.model.Activity;
import com.choosefine.springboot.devops.activity.service.ActivityService;
import com.choosefine.springboot.devops.activityImg.model.ActivityImg;
import com.choosefine.springboot.devops.activityImg.service.ActivityImgService;
import com.choosefine.springboot.devops.base.config.MapUtil;
import com.choosefine.springboot.devops.base.result.ReturnMSG;
import com.choosefine.springboot.devops.base.serviceImpl.RedisServiceImpl;
import com.choosefine.springboot.devops.base.weixin.Event;
import com.choosefine.springboot.devops.participate.dto.InsertParticipateDTO;
import com.choosefine.springboot.devops.participate.dto.UpdateParticipateDTO;
import com.choosefine.springboot.devops.participate.model.Participate;
import com.choosefine.springboot.devops.participate.model.RedisParticipate;
import com.choosefine.springboot.devops.participate.service.ParticipateService;
import com.choosefine.springboot.devops.participate.vo.ParticipateAndActivityVo;
import com.choosefine.springboot.devops.user.model.User;
import com.choosefine.springboot.devops.user.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/participate")
public class ParticipateController {
	@Autowired
	private ParticipateService participateService;
	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	private RedisServiceImpl redisService;
	@Autowired
	private ActivityService activityService;
	@Autowired
	private ActivityImgService activityImgService;
	@Autowired
	private UserService userService;
	@Autowired
	private Event event;
	@Autowired
	private RestTemplate restTemplate;

	@GetMapping("/{id}")
	public String selectById(@PathVariable("id") Integer id,HttpServletResponse response) throws IOException {
//		event.event("participate/"+id,response);
//		event.event("activity/"+44,response,);

		ReturnMSG returnMSG=new ReturnMSG();
		if(id==null){
			return objectMapper.writeValueAsString(returnMSG.error());
		}
		String participateString=redisService.get("Participate"+id);
		if(participateString==null){
			return objectMapper.writeValueAsString(returnMSG.error());
		}
		Participate participate=objectMapper.readValue(participateString,Participate.class);
		String activityString=redisService.get("Activity"+participate.getId());
		if(activityString==null){
			return objectMapper.writeValueAsString(returnMSG.error());
		}
		Activity activity=objectMapper.readValue(activityString,Activity.class);
		User user=userService.selectById(participate.getUserId());
		if(user==null){
			return objectMapper.writeValueAsString(returnMSG.error());
		}
		List<ActivityImg> activityImgs =activityImgService.selectByActivityId(participate.getActivityId());
		ParticipateAndActivityVo result=new ParticipateAndActivityVo();
		result.setActivity(activity);
		result.setActivityImgList(activityImgs);
		result.setParticipate(participate);
		result.setUser(user);
		return objectMapper.writeValueAsString(result);
	}

	@PostMapping()
	public String save(@Valid InsertParticipateDTO insertParticipateDTO, BindingResult result, HttpServletResponse response) throws IOException {
		ReturnMSG returnMSG=new ReturnMSG();
		if(result.hasErrors()){
			return objectMapper.writeValueAsString(returnMSG.paramError());
		}else{
			if(insertParticipateDTO.getWxCode()==null){
				event.event("activity/"+insertParticipateDTO.getActivityId(),response,insertParticipateDTO.getActivityId());
				return null;
			}else{
				User user=userService.selectByCode(insertParticipateDTO.getWxCode());
				if(user ==null || user.getStatus()!=1){
					event.event("activity/"+insertParticipateDTO.getActivityId(),response,insertParticipateDTO.getActivityId());
					return null ;
				}
				Activity activity=activityService.selectById(insertParticipateDTO.getActivityId());
				if(activity.getNumber()==0){
					return objectMapper.writeValueAsString(returnMSG.error("没有名额了！"));
				}
				if(participateService.selectByActAndWxCode(insertParticipateDTO.getWxCode(),insertParticipateDTO.getActivityId())!=null){
					return objectMapper.writeValueAsString(returnMSG.error("不能重复参加！"));
				}
				Participate param=new Participate();
				BeanUtils.copyProperties(insertParticipateDTO,param);
				param.setCreateTime(new Date());
				param.setUserId(user.getId());
				Integer resultId=participateService.save(Participate.class, MapUtil.transBean2Map(param));
				if(resultId>0){
					RedisParticipate redisParam=new RedisParticipate();
					BeanUtils.copyProperties(activity,redisParam);
					BeanUtils.copyProperties(param,redisParam);
					redisService.set("Participate"+resultId,objectMapper.writeValueAsString(redisParam));
				}
				return objectMapper.writeValueAsString(returnMSG.success(param));
			}
		}
	}

	@GetMapping("/")
	public String selectList() throws JsonProcessingException {
		List<Participate> list=participateService.selectList(1,10);
		return objectMapper.writeValueAsString(list);
	}

	@PutMapping()
	public String update(@Valid  UpdateParticipateDTO updateParticipateDTO, BindingResult result) throws JsonProcessingException {
		ReturnMSG returnMSG=new ReturnMSG();
		if(result.hasErrors()){
			return objectMapper.writeValueAsString(returnMSG.error());
		}
		if(participateService.update(Participate.class,MapUtil.transBean2Map(updateParticipateDTO))>0){
			return objectMapper.writeValueAsString(returnMSG.success());
		}else{
			return objectMapper.writeValueAsString(returnMSG.error());
		}
	}

	@DeleteMapping("/{id}")
	public String delete(@PathVariable Integer id) throws JsonProcessingException {
		ReturnMSG returnMSG=new ReturnMSG();
		if(id==null){
			return objectMapper.writeValueAsString(returnMSG.paramError());
		}else{
			Map param=new HashMap();
			param.put("id",id);
			param.put("status",2);
			if(participateService.falseDelete(Participate.class,param)>0){
				return objectMapper.writeValueAsString(returnMSG.success());
			}else{
				return objectMapper.writeValueAsString(returnMSG.error());
			}
		}
	}

}
